package com.ztewatch.wearcontacts.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ztewatch.wearcontacts.R;
import com.ztewatch.wearcontacts.data.EmergencyContactItem;

public class EmergencyContactAdapter extends BaseAdapter {
	private List<EmergencyContactItem> emergencyContactList;
	private Context mContext;
	private LayoutInflater mInflater;
	private int cur_pos = 0;

	public EmergencyContactAdapter(Context context,
			ArrayList<EmergencyContactItem> contactList) {
		emergencyContactList = contactList;
		mContext = context;
		mInflater = LayoutInflater.from(context);
	}

	public void setCurPosition(int pos) {
		cur_pos = pos;
	}

	@Override
	public int getCount() {
		return emergencyContactList.size();
	}

	@Override
	public Object getItem(int position) {
		return emergencyContactList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(
					R.layout.emergency_contact_list_item, null);
			holder = new ViewHolder();
			holder.mTextView_num = (TextView) convertView
					.findViewById(R.id.emergency_contact_number);
			holder.mTextView_name = (TextView) convertView
					.findViewById(R.id.emergency_contact_name);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		EmergencyContactItem mEmergencyContactItem = emergencyContactList
				.get(position);

		holder.mTextView_num.setText(mContext.getResources().getString(
				R.string.edit_contact_emergency_contact_num, position));
		if (position == 0) {
			holder.mTextView_num.setVisibility(View.GONE);
		} else {
			holder.mTextView_num.setVisibility(View.VISIBLE);
		}
		holder.mTextView_name.setVisibility(View.VISIBLE);
		if (position == mEmergencyContactItem.getmStarred()) {
			holder.mTextView_name.setText(mEmergencyContactItem.getmName());
		} else {
			holder.mTextView_name.setVisibility(View.INVISIBLE);
		}

		if (position == cur_pos) {
			convertView.setBackgroundColor(Color.LTGRAY);
		} else {
			convertView.setBackgroundColor(Color.TRANSPARENT);
		}

		return convertView;
	}

	private class ViewHolder {
		TextView mTextView_num;
		TextView mTextView_name;
	}
}
